package com.example.xmhds_1.controller;

import com.example.xmhds_1.common.Result;
import com.example.xmhds_1.entity.UserAccount;
import com.example.xmhds_1.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 公开接口控制器
 * 包含登录等不需要认证的接口
 */
@Api(tags = "公开接口")
@RestController
@RequestMapping("/api/public")
@RequiredArgsConstructor
public class PublicController {

    private final UserAccountService userAccountService;

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return 登录结果
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserAccount> login(
            @RequestParam String username,
            @RequestParam String password) {
        UserAccount user = userAccountService.login(username, password);
        if (user != null) {
            // 登录成功，清空密码
            user.setPassword(null);
            return Result.success(user, "登录成功");
        } else {
            return Result.error("用户名或密码错误");
        }
    }
}
